home *** CD-ROM | disk | FTP | other *** search
/ Top 200 Programs / Top 200 Programs.iso / Bob8 / THOMPSON / LIBERTY / PRODUCT / TUTORIAL.EXE / WK2HW1.TXT < prev    next >
Text File  |  1996-12-04  |  7KB  |  222 lines

  1. Week 2 Homework 1
  2. Liberty BASIC programming course
  3. Copyright 1996 Shoptalk Systems
  4. All Rights Reserved
  5.  
  6. FOR/NEXT Loops
  7. ==============================================================================
  8.  
  9. Up to this point most of the loops that we've used in our code have had this
  10. form:
  11.  
  12.     variable = 0
  13. [loopPoint]
  14.     'put some code in here
  15.     variable = variable + 1
  16.     if variable < 10 then [loopPoint]
  17.  
  18. This will loop 10 times.  The first time through the loop the value of
  19. variable will be 0, and the tenth time through the loop it will be 9.  This
  20. kind of coding does work, but it can look complicated and it takes some
  21. care to make sure it is coded properly (it's easy to make a mistake).  Now
  22. let's take a look at an alternative:
  23.  
  24.     for variable = 0 to 9
  25.         'put some code here
  26.     next variable
  27.  
  28. This is called a FOR/NEXT loop.  The code that is inserted between the FOR
  29. and NEXT statement will be executed 10 times.  The value of variable will
  30. be 0 the first time through and 9 the last time.
  31.  
  32. We can use this kind of code to do something a specifed number of times or
  33. to do something using a range of values (for example 0 to 9).
  34.  
  35. FOR/NEXT loops require fewer lines of code.  When programming, it is
  36. usually best to use the simplest possible way to code any solution.  This
  37. kind of simplicity doesn't come without a price.  There are things you can
  38. do with the first kind of looping that you cannot do with a FOR/NEXT loop.
  39.  
  40. Look at this example:
  41.  
  42.     variable = 0
  43. [loopPoint]
  44.     'put your code here
  45.     if someCondition = 1 then [specialException]
  46.     variable = variable + 1
  47.     if variable < 10 then [loopPoint]
  48.  
  49. Notice the line of code that branches to [specialException] if someCondition
  50. is equal to 1.  This is perfectly acceptable coding practice.  Now look at
  51. this example:
  52.  
  53.     for variable = 0 to 9
  54.         'put your code here
  55.         if someCondition = 1 then [specialException]
  56.     next variable
  57.  
  58. This is not acceptable.  Liberty BASIC expects the loop to finish properly.
  59. It is OK to use GOSUB to call a subroutine from inside a FOR/NEXT loop
  60. because when the subroutine returns, execution will resume inside the loop
  61. and it will be allowed to finish properly.
  62.  
  63. A good question to ask when deciding whether to use a FOR/NEXT loop is "Do
  64. I know how many times this loop will be executed?"  In our example above
  65. we don't know because at any time someCondition could suddenly be equal to
  66. 1, and then our loop is finished.
  67.  
  68. You can cheat.  If you set the value of variable to its finishing value (or
  69. limit), the loop will think it's all done and it will quit.  For example the
  70. following code will only count to 5.
  71.  
  72.     for x = 0 to 9
  73.         print x
  74.         if x = 5 then x = 9
  75.     next x
  76.  
  77. It's also worth noting that FOR/NEXT loops don't need to use integer values.
  78. Here is an example that counts from 0 to 1 in steps of less than 1.
  79.  
  80.     for i = 0 to 1 step 0.04
  81.         print i
  82.     next i
  83.  
  84. By using the word STEP and a value we specified what value to add to our
  85. variable (i in this case).  We can count backwards using a similar
  86. technique.
  87.  
  88.     for j = 10 to 1 step -1
  89.         print j
  90.     next j
  91.  
  92. Finally, if a starting value for the variable in our FOR/NEXT loop is
  93. greater than its limit, the code between the FOR and NEXT statements will be
  94. skipped over.  The following code will not display any numbers at all.
  95.  
  96.     for x = 1 to 0
  97.         print x  'this line will not be executed
  98.     next x
  99.  
  100. Similarly...
  101.  
  102.     for x = 0 to 1 step -1
  103.         print x  'this line will not be executed
  104.     next x
  105.  
  106. Here is our ARRAYS.BAS program taken from our Week 2 Solutions to Challenge
  107. Exercises.  We've modified it by replacing one of the loops with a FOR/NEXT
  108. loop.  Study it carefully.
  109.  
  110.  
  111.     'ARRAYS.BAS
  112.     'List handling with arrays
  113.     'Here is a version that displays the names entered
  114.     'after [quit]
  115.  
  116.     dim names$(10)  'set up our array to contain 10 items
  117.  
  118. [askForName]  'ask for a name
  119.     input "Please give me your name ?"; yourName$
  120.     if yourName$ = "" then print "No name entered." : goto [quit]
  121.  
  122.     index = 0
  123. [insertLoop]
  124.     'check to see if index points to an unused item in the array
  125.     if names$(index) = "" then names$(index) = yourName$ : goto [nameAdded]
  126.     index = index + 1 'add 1 to index
  127.     if index < 10 then [insertLoop] 'loop back until we have counted to 10
  128.  
  129.     'There weren't any available slots, inform user
  130.     print "All ten name slots already used!"
  131.     goto [quit]
  132.  
  133. [nameAdded]  'Notify the name add was successful
  134.  
  135.     print yourName$; " has been added to the list."
  136.     highestSlot = index  'this is a new line of code
  137.     goto [askForName]
  138.  
  139. [quit]
  140.  
  141.     'display all the entered names
  142.     print
  143.     print "Here is a list of the names entered:"
  144.     print "------------------------------------"
  145.     for index = 0 to highestSlot
  146.          if names$(index) <> "" then print names$(index)
  147.     next index
  148.  
  149.     end
  150.  
  151.  
  152. Here's the assignment
  153. -------------------------------------------------------------------------
  154.  
  155. 1) In similar fashion to our modification of ARRAYS.BAS (above), modify
  156. the AGES.BAS program below, replacing appropriate looping code with
  157. FOR/NEXT loops.
  158.  
  159.     'AGES.BAS
  160.     'Accept some names and ages from the user, then total and average them
  161.     dim numbers(20)
  162.     dim names$(20)
  163.     print "AGES.BAS"
  164.     print
  165.  
  166.     'loop up to 20 times, getting numbers
  167.     print "Enter up to 20 non-zero values."
  168.     print "A zero or blank entry ends the series."
  169.  
  170. [entryLoop]  'loop around until a zero entry or until index = 20
  171.  
  172.     'get the user's name and age
  173.     print "Entry "; index + 1;
  174.     input name$
  175.     if name$ = "" then [endSeries]  'quit if name$ is blank
  176.     print "Age   ";
  177.     input age
  178.  
  179.     index = index + 1       'add one to index
  180.     names$(index) = name$   'set the specified array item to be name$
  181.     numbers(index) = age    'set the specified array item to be age
  182.     total = total + age     'add entry to the total
  183.  
  184.     if index = 20 then [endSeries]  'if 20 values were entered, exit loop
  185.  
  186.     goto [entryLoop]  'go back and get another entry
  187.  
  188. [endSeries]  'entries are finished
  189.  
  190.     'Set entryCount to index
  191.     entryCount = index
  192.     if entryCount = 0 then print "No Entries." : goto [quit]
  193.  
  194.     print "Entries completed."
  195.     print
  196.     print "Here are the "; entryCount; " entries:"
  197.     print "-----------------------------"
  198.  
  199.     'This loop displays each entered value in turn.
  200.     'Notice that we re-use the index variable.  It
  201.     'can be confusing to use a new variable for each
  202.     'new loop.    
  203.     index = 0
  204. [displayLoop]
  205.     index = index + 1
  206.     print "Entry "; index; " is "; names$(index); ", age "; numbers(index)
  207.     if index < entryCount then [displayLoop]
  208.  
  209.     'Now display the total and average value
  210.     print
  211.     print "The total age is "; total
  212.     print "The average age is "; total / entryCount
  213.  
  214. [quit]
  215.  
  216.     end
  217.  
  218.  
  219. 2) Add a routine onto the end of AGES.BAS that asks the user for a name.
  220. Then using a FOR/NEXT loop searches for that name in the names$ array and
  221. if the name is found displays the age for that name.
  222.